﻿// Created by amoylel on 11/12/2018.
// Copyright (c) 2018 amoylel All rights reserved.

#ifndef AMO_PIPE_LINUX_4F2F5611_8413_4457_8E19_AAD1D2B762CB_HPP__
#define AMO_PIPE_LINUX_4F2F5611_8413_4457_8E19_AAD1D2B762CB_HPP__








#include <string>
#include <cstdint>
#include <iostream>
#include <amo/config.hpp>
#include <amo/object.hpp>

#ifndef PipeNamePrefix
#define PipeNamePrefix "\\\\.\\pipe\\"
#endif


namespace amo {

    enum pipe_type {
        server,
        client,
    };
    
    class pipe_base : public object {
    public:
        int64_t read(void* buffer, int64_t buffer_size) {
            return buffer_size;
        }
        
        bool is_ready() {
            return true;
        }
        
        int64_t write(const void* buffer, int64_t buffer_size) {
            return buffer_size;
            
        }
        
        virtual bool connect() {
            return false;
        }
    protected:
        pipe_base() {
        
        }
    protected:
    
        std::string pipe_name;		//管道名称
    };
    
    template<amo::pipe_type> class pipe;
    
    
    template<> class pipe<amo::server> : public pipe_base {
    public:
        pipe(std::string pipe_name_, int buffer_size = 10000000) {
            pipe_name = std::string(PipeNamePrefix) + pipe_name_;
            connected = true;
            return;
        }
        
        ~pipe() {
            //后处理
            
        }
        
        // 连接管道，阻塞
        virtual bool connect() {
            if (connected) {
                return true;
            }
            
            
            
            return connected;
        }
        
    private:
        bool connected;
    };
    
    
    
    //管道客户端
    
    template<> class pipe<amo::client> : public pipe_base {
    public:
        pipe(std::string pipe_name_) {
            pipe_name = std::string(PipeNamePrefix) + pipe_name_;
        }
        
        // 连接pipe,服务端未开连接失败
        virtual bool connect() {
        
        
            return true;
        }
        
        
        ~pipe() {
        
        }
    };
    
}


#endif //AMO_PIPE_LINUX_4F2F5611_8413_4457_8E19_AAD1D2B762CB_HPP__